<!DOCTYPE html>
<html>

<head lang="en">
    <title>积分商城</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategy.css"/>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>

    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/common.js"></script>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>

</head>

<body>
<div class="search-head">
    <!--<div class="bk21" id="mess_box" style="overflow-y:auto;overflow:scroll; position:absolute;top=0px; left:0px;">-->
    <div class="row nav-search" style="background-color: yellowgreen">
        <div class="col-12">
            <a href="/mine/profiles.html">
                <span class="headInfo"><i class="fa fa-chevron-left fa-1x"></i>

                </span>
            </a>
        </div>
    </div>
    <div>
        <img src="/img/u=3066882233,3496844441&fm=26&gp=0.jpg" width="100%">
    </div>
</div>

<ul class="nav nav-pills nav-justified border border-left-0 border-top-0 border-right-0" id="pills-tab" role="tablist">
    <li class="nav-item">
        <a class="nav-link active show" href="#pills-top">
            <span style="font-weight: bold;">推荐</span>
        </a>
    </li>
    <li class="nav-item">
        <a class="nav-link active show" href="/mine/integralRecord.html">
            <span style="font-weight: bold;">兑换记录</span>
        </a>
    </li>

</ul>


<div class="tab-content" id="pills-tabContent">
    <div class="tab-pane fade active show" id="pills-top">
        <hr>
        <div class="container strategyCommend ">
            <h6 style="text-align: center;background-color: #F9DF3B">积分兑换</h6><br/>
            <div class="row hot" render-loop="list">
                <div class="col-6">
                    <a role="button" class="payBtn" render-attr="data-id=list.id">
                        <img render-src="list.productImgUrl">
                        <p render-html="list.product"></p>
                        <p style="background-color: #F9DF3B">所需积分<span render-html="list.productIntegral"></span></p>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $("#pills-top-tab2").click(function () {
            console.log(1);
        })

        var currentPage = 1;
        var pages;
        var user = JSON.parse(sessionStorage.getItem("user"));
        //剩余积分
        var result;
        if (user) {
            $.get("/days/points", {userId: user.id}, function (data) {
                $(".headInfo").append("--尊敬的用户:[ " + user.nickName + " ]剩余积分:" + data);
                result = data;
            })
        }
        var store = [];

        //界面显示
        function query() {
            $.get("/integralStores", {currentPage: currentPage, pageSize: 6}, function (data) {
                $.merge(store, data.list);
                var datas = {"list": store};
                $(".strategyCommend").renderValues(datas);
                pages = data.pages;
                //点击图片支付
                $(".payBtn").click(function () {
                    console.log($(this).data("id"));
                    //积分商品的id
                    var id = $(this).data("id");
                    //商品的信息
                    $.get("/integralStores/" + id, function (data) {
                        $(document).dialog({
                            type: 'confirm',
                            closeBtnShow: true,
                            content: '你确定要消耗' + data.productIntegral + "积分兑换" + data.product + "吗",
                            onClickConfirmBtn: function () {
                                console.log("这里是确定", id, user.id, data.productIntegral);
                                if(result>=data.productIntegral){
                                    $.post("/integralStores", {
                                        userId: user.id,
                                        productId: id,
                                        productIntegral: data.productIntegral
                                    }, function (data) {
                                        console.log(data.succeed);
                                        if (data.succeed ) {
                                            $(document).dialog({
                                                titleText: '温馨提示',
                                                content: '兑换成功',
                                            })
                                            setTimeout(function () {
                                                window.location.reload()
                                            }, 1000)
                                        }
                                    });
                                }else {
                                    $(document).dialog({
                                        titleText: '温馨提示',
                                        content: '积分不足',
                                    })
                                }


                            },
                            onClickCancelBtn: function () {
                            },
                        });
                    })
                })
            });
            currentPage++;
        }

        query();
        //滚动条事件
        $(window).scroll(function () {
            //页面滑动查询
            if (($(window).height() + $(document).scrollTop()) >= $(document).height() - 1) {
                if (currentPage <= pages) {
                    query();
                } else {
                    $(document).dialog({
                        type: 'notice',
                        infoText: '已经到底了',
                        autoClose: 1000,
                        position: 'bottom'  // center: 居中; bottom: 底部
                    });
                }
            }
        });

    })

</script>
</body>

</html>